package com.maila.simplerecord.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ld on 2016/4/26.
 */
public class DateTFormat {

    public int getNewDateShow(String date) {
        if (date == null) {
            return 0;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        Date now = new Date(System.currentTimeMillis());
        int is = 0;
        try {
            now = df.parse(df.format(now));
            Date createdate = df.parse(date);
            int year = now.getYear() - createdate.getYear();
            int month = now.getMonth() - createdate.getMonth();
            int sume = 12 * year + month;
            int years = sume / 12;
            int months = sume % 12;
            if (months > 5) {
                is = years + 1;
            } else if (months < 0) {
                is = years - 1;
            } else {
                is = years;
            }
        } catch (ParseException e) {
//            showdate = date;
            e.printStackTrace();
        }

        return is;
    }

    public static String getDate(Date Dates) {

        if (Dates == null)
            throw new RuntimeException("日期不能为null");

        int year = 0;
        int month = 0;
        Date now = new Date();
        SimpleDateFormat format_y = new
                SimpleDateFormat("yyyy");
        SimpleDateFormat format_M = new
                SimpleDateFormat("MM");

        String date_year =
                format_y.format(Dates);
        String this_year =
                format_y.format(now);

        String date_month =
                format_M.format(Dates);
        String this_month =
                format_M.format(now);


        year = Integer.parseInt(this_year) - Integer.parseInt(date_year);
        month = 12 - Integer.parseInt(date_month) + Integer.parseInt(this_month);
        if (this_month.compareTo(date_month) < 0) {
            year -= 1;
        }
        if (year < 0 || year == 0) {
            return month + "个月";
        }
        if (month == 0) {
            return year + "年";
        } else {
            return year + "年" + month + "个月";
        }

    }
public  static  boolean  suitDate(int year,int month,int day){
    SimpleDateFormat format_y = new
            SimpleDateFormat("yyyy");
    SimpleDateFormat format_M = new
            SimpleDateFormat("MM");
    SimpleDateFormat format_d = new
            SimpleDateFormat("DD");
    int nowyear=Integer.parseInt(format_y.format(new Date()));
    int  nowmoth=Integer.parseInt(format_M.format(new Date()));
    int nowDay=Integer.parseInt(format_d.format(new Date()));
    if(year>nowyear){
        return false;
    }else {
        if(year==nowyear&&month>nowmoth){
            return false;
        }else {
            if(month==nowmoth){
                return day <= nowDay;
            }
            return  true;
        }
    }

}

    /**
     * 日期转化成字符串
     *
     * @param Dates
     * @return
     */
    public static String date2String(Date Dates) {
        String date = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd ");
        date = df.format(Dates);
        return date;
    }

    /**
     * 日期转化成字符串
     *
     * @param Dates
     * @return
     */
    public static String showTime(Date Dates) {
        String date = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        date = df.format(Dates);
        return date;
    }

    /**
     * 日期转时间字符串
     *
     * @param Dates
     * @return
     */
    public static String date2TimeString(Date Dates) {
        String date = null;
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        date = df.format(Dates);
        return date;
    }

    /**
     * 字符串转化日期
     *
     * @param date
     * @return
     */
    public static Date timeString2Date(String date) {
        Date dates = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = date.replace("T", " ");
            dates = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dates;
    }
    public static String getPushTime(String  date){
        String time="";
        Date senddata = DateTFormat.timeString2Date(date);
        Date now = new Date();
        if ((now.getTime() - senddata.getTime()) / (1000 * 60 * 60) > 24) {
            time=((now.getTime() - senddata.getTime()) / (1000 * 60 * 60 * 24)) + "天前";

        } else if ((now.getTime() - senddata.getTime()) / (1000 * 60 * 60) < 1) {
            time=((now.getTime() - senddata.getTime()) / (1000 * 60)) + "分钟前";
        } else if ((now.getTime() - senddata.getTime()) / (1000 * 60) < 1) {
            time="刚刚";
        } else {
            time=((now.getTime() - senddata.getTime()) / (1000 * 60 * 60)) + "小时前";
        }

        return time;
    }


    /**
     * 字符串转化日期
     *
     * @param date
     * @return
     */
    public static Date string2Date(String date) {
        Date dates = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dates = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dates;
    }

    public static boolean compareDateOneDay(Date A, Date B) {
        if (A == null || B == null) {
            return false;
        }

        long timeLong = B.getTime() - A.getTime();
        timeLong = timeLong / 1000 / 60 / 60 / 24;
        return timeLong > 1;
    }

    public static String getNDateShow(String date) {
        if (date == null) {
            return null;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String showdate = "";

        try {
            now = df.parse(df.format(now));
            Date createdate = df.parse(date);
            long l = now.getTime() - createdate.getTime();

            long day = l / (24 * 60 * 60 * 1000);
            if (day == 0) {
                DateFormat df_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date createdate_time = df_time.parse(date);
                showdate = df_time.format(createdate_time);
                showdate = showdate.substring(11);
            } else if (day == 1) {
                showdate = "昨天";
            } else if (day > 1 && day <= 7) {
                DateFormat df_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date createdate_time = df_time.parse(date);
                DateFormat df_time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
                showdate = df_time2.format(createdate_time);
                showdate = showdate.substring(20);
            } else if (day > 7) {
                showdate = df.format(createdate);
            }

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            showdate = date;
        }

        return showdate;
    }


    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /*将字符串转为时间戳*/
    public static long getStringToDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }
}
